package com.mhy.tvcontrol

import android.bluetooth.BluetoothDevice
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.MotionEvent
import android.widget.FrameLayout
import android.widget.Toast
import com.dlong.rep.dlroundmenuview.DLRoundMenuView
import com.dlong.rep.dlroundmenuview.Interface.OnMenuTouchListener


/**
 * Created By Mahongyin
 * Date    2022/3/15 18:02
 *
 */
class BuleCarControlView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null,
    defStyle: Int = 0
) : FrameLayout(context, attrs, defStyle) {
    companion object {
        val byteup = "ONA".toByteArray()
        val bytedown = "ONB".toByteArray()
        val bytestop = "ONF".toByteArray()
        val byteleft = "ONC".toByteArray()
        val byteright = "OND".toByteArray()
    }

    private val REQUEST_CONNECT_DEVICE = 1
    private val REQUEST_ENABLE_BT = 0


    private val KEYCODE_CENTER = -1
    private val KEYCODE_UP = 0
    private val KEYCODE_RIGHT = 1
    private val KEYCODE_DOWN = 2
    private val KEYCODE_LEFT = 3
    private var isconnect = false
    private var mBluetoothService: BluetoothService? = null

    private val myhandler: Handler = object : Handler(Looper.getMainLooper()) {
        override fun handleMessage(msg: Message) {
            when (msg.what) {
                BluetoothService.MESSAGE_TOAST -> {
                    val str = msg.data.getString(BluetoothService.TOAST)
                    Toast.makeText(getContext(), str, Toast.LENGTH_LONG).show()
                    return
                }
                else -> return
            }
        }
    }

    init {
        mBluetoothService = BluetoothService(getContext(), myhandler)
        LayoutInflater.from(context).inflate(R.layout.blue_control, this, true)
        findViewById<DLRoundMenuView>(R.id.dl_rmv)?.setOnMenuTouchListener(object :
            OnMenuTouchListener {
            override fun OnTouch(event: MotionEvent?, position: Int) {
                if (event != null && BluetoothService.connectedThread != null) {
                    when (position) {
                        KEYCODE_UP ->
                            when (event.action) {
                                REQUEST_ENABLE_BT -> {
                                    send(byteup)

                                }
                                REQUEST_CONNECT_DEVICE -> {
                                    send(bytestop)

                                }
                                else -> {
                                }
                            }
                        KEYCODE_LEFT -> {
                            when (event.action) {
                                REQUEST_ENABLE_BT -> {
                                    send(byteleft)

                                }
                                REQUEST_CONNECT_DEVICE -> {
                                    send(bytestop)

                                }
                                else -> {
                                }
                            }
                        }
                        KEYCODE_CENTER -> {
                            when (event.action) {
                                REQUEST_ENABLE_BT -> {
                                    send("ONE".toByteArray())
                                }
                                REQUEST_CONNECT_DEVICE -> {
                                    send(bytestop)
                                }
                                else -> {
                                }
                            }
                        }
                        KEYCODE_RIGHT -> {
                            when (event.action) {
                                REQUEST_ENABLE_BT -> {
                                    send(byteright)

                                }
                                REQUEST_CONNECT_DEVICE -> {
                                    send(bytestop)

                                }
                                else -> {
                                }
                            }
                        }
                        KEYCODE_DOWN -> {
                            when (event.action) {
                                REQUEST_ENABLE_BT -> {
                                    send(bytedown)

                                }
                                REQUEST_CONNECT_DEVICE -> {
                                    send(bytestop)

                                }
                                else -> {
                                }
                            }
                        }
                        else -> {
                        }
                    }
                } else {
                    Toast.makeText(getContext(), "蓝牙未连接", Toast.LENGTH_LONG).show()
                }
            }

        })
    }

    fun connect(device: BluetoothDevice) {
        mBluetoothService?.connect(device)
        isconnect = true
    }

    fun disConnect() {
        mBluetoothService?.cancelThread()
        isconnect = false
    }

    fun send(bytes: ByteArray) {
        if (BluetoothService.connectedThread != null && isconnect) {
            BluetoothService.connectedThread.write(bytes)
        }
    }
}